package com.salary.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.salary.dao.SalaryFormulaRepository;
import com.salary.entity.SalaryFormula;
import com.salary.entity.dto.CountDayDTO;
import com.salary.entity.dto.SalaryFormulaDTO;

@Service
@Transactional
public class SalaryFormulaService implements ISalaryFormulaService {
	@Autowired
	private SalaryFormulaRepository salaryFormulaDao;

	public List<SalaryFormulaDTO> findDTO() {
		List<SalaryFormula> sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		List<SalaryFormulaDTO> list = new ArrayList<SalaryFormulaDTO>();
		if (list.size() == 0) {
			createNewFormula();
		}
		sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		list = SalaryFormulaDTO.getFormula(sfs.get(0));
		return list;
	}

	public void saveOrUpdate(SalaryFormulaDTO dto) {
		System.out.println("dto信息："+dto.getId()+dto.getParam()+dto.getDays());
		Long id = dto.getId();
		if (id == null)
			return;
		List<SalaryFormula> sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		if (sfs.size() == 0) {
			createNewFormula();
		}
		sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		SalaryFormula formula = sfs.get(0);
		if (id == 1) {
			System.out.println("更新ID1的信息，DTO.Param"+dto.getParam()+"此时formula的absenceIdx："+formula.getAbsenceIdx());
			formula.setDays(dto.getDays());
			formula.setAbsenceIdx(dto.getParam());
		} else if (id == 2) {
			formula.setDays(dto.getDays());
			formula.setLeaveIdx(dto.getParam());
		} else if (id == 3) {
			formula.setDays(dto.getDays());
			formula.setUsualOTIdx(dto.getParam());
		} else if (id == 4) {
			formula.setDays(dto.getDays());
			formula.setWkdOTIdx(dto.getParam());
		} else if (id == 5) {
			formula.setDays(dto.getDays());
			formula.setLvOTIdx(dto.getParam());
		} else {
			formula.setPerfIdx1(dto.getDays());
			formula.setPerfIdx2(dto.getParam());
		}
		salaryFormulaDao.save(formula);
	}

	public SalaryFormula findOne(Long id) {
		SalaryFormula sf = salaryFormulaDao.findOne(id);
		return sf;
	}

	public void setCountDay(Integer countDay) {
		List<SalaryFormula> sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		if (sfs.size() == 0)createNewFormula();
		if (countDay != null && countDay > 0 && countDay <= 28) {
			sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
			SalaryFormula f = sfs.get(0);
			f.setCountDay(countDay);
			salaryFormulaDao.save(f);
		}
	}

	public void createNewFormula() {
		List<SalaryFormula> list = (List<SalaryFormula>) salaryFormulaDao.findAll();
		if (list.size() == 0) {
			SalaryFormula f = new SalaryFormula();
			f.setAbsenceIdx(1);
			f.setCountDay(7);
			f.setDays(21.75f);
			f.setLeaveIdx(1);
			f.setLvOTIdx(3);
			f.setPerfIdx1(1);
			f.setPerfIdx2(1);
			f.setUsualOTIdx(1.5f);
			f.setWkdOTIdx(2);
			System.out.println("这个ID"+f.getId());
			salaryFormulaDao.save(f);
		}
	}

	public List<CountDayDTO> findCountDayDTO() {
		List<SalaryFormula> sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		if(sfs.size() == 0) {
			createNewFormula();
			sfs = (List<SalaryFormula>) salaryFormulaDao.findAll();
		}
		return CountDayDTO.getcountDayDTO(sfs.get(0));
	}

}
